package com.compass.framework.utils

import android.app.Activity
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Matrix
import android.graphics.Outline
import android.net.Uri
import android.os.Build
import android.provider.MediaStore
import android.view.View
import android.view.ViewOutlineProvider
import android.view.WindowManager
import java.io.File
import java.io.FileOutputStream


/**
 * View相关处理工具类
 */
object ViewUtils {

    /**
     * 设置View圆角
     */
    fun setClipViewCornerRadius(view: View?, radius: Int) {
        if (view == null) {
            return
        }
        if (radius > 0) {
            view.outlineProvider = object : ViewOutlineProvider() {
                override fun getOutline(view: View?, outline: Outline?) {
                    outline?.setRoundRect(0, 0, view?.width ?: 0, view?.height ?: 0, radius.toFloat())
                }
            }
            view.clipToOutline = true
        } else {
            view.clipToOutline = false
        }
    }

    /**
     * 设置View顶部圆角
     */
    fun setClipViewCornerTopRadius(view: View?, radius: Int) {
        if (view == null) {
            return
        }
        if (radius > 0) {
            view.outlineProvider = object : ViewOutlineProvider() {
                override fun getOutline(view: View?, outline: Outline?) {
                    outline?.setRoundRect(0, 0, view?.width ?: 0, view?.height?.plus(radius) ?: 0, radius.toFloat())
                }
            }
            view.clipToOutline = true
        } else {
            view.clipToOutline = false
        }
    }

    /**
     * 设置View底部圆角
     */
    fun setClipViewCornerBottomRadius(view: View?, radius: Int) {
        if (view == null) {
            return
        }
        if (radius > 0) {
            view.outlineProvider = object : ViewOutlineProvider() {
                override fun getOutline(view: View?, outline: Outline?) {
                    outline?.setRoundRect(0, -radius, view?.width ?: 0, view?.height ?: 0, radius.toFloat())
                }
            }
            view.clipToOutline = true
        } else {
            view.clipToOutline = false
        }
    }

    /**
     * 设置透明沉浸式状态栏
     * @param activity 页面
     * @param isDarkText 是否状态栏文字显示深色
     */
    fun transparentBar(activity: Activity, isDarkText: Boolean = false) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val window = activity?.window
            window?.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            window?.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window?.statusBarColor = Color.TRANSPARENT
            var systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            if (isDarkText && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                systemUiVisibility = systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            }
            window?.decorView?.systemUiVisibility = systemUiVisibility
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            val window = activity?.window
            window?.setFlags(
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
            )
        }
    }

    /**
     * 高效的获取View的裁剪区
     * @param view 需要处理的View
     * @param downscaleFactor 缩放参数
     * @return
     */
    fun getDownscaledBitmapForView(
        view: View,
        downscaleFactor: Float
    ): Bitmap? {
        val width = (view.width * downscaleFactor).toInt()
        val height = (view.height * downscaleFactor).toInt()

        if (width * height <= 0) {
            return null
        }
        val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) //准备图片

        val canvas = Canvas(bitmap) //将bitmap作为绘制画布
        val matrix = Matrix()
        matrix.preScale(downscaleFactor, downscaleFactor)
        canvas.setMatrix(matrix) //设置matrix
        canvas.drawColor(Color.WHITE)
        view.draw(canvas) //讲View特定的区域绘制到这个canvas（bitmap）上去，
        return bitmap //得到最新的画布
    }


}